package tg.app.opticket;

import java.rmi.RemoteException;
import java.text.DecimalFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashSet;

import psdi.mbo.MboRemote;
import psdi.mbo.MboSet;
import psdi.mbo.MboSetRemote;
import psdi.mbo.MboValueInfo;
import psdi.mbo.StatefulMbo;
import psdi.mbo.StatusHandler;
import psdi.server.AppService;
import psdi.util.MXException;

/***
 * 
 * @author pzx
 * 
 */
public class OpTicket extends StatefulMbo implements OpTicketRemote {
	private static final long serialVersionUID = 1L;
	private static HashSet skipFieldCopy = new HashSet();
	private static boolean isHashSetLoaded = false;

	public OpTicket(MboSet mboset) throws RemoteException {
		super(mboset);
	}

	public void init() throws MXException {
		super.init();
		try {
			if (toBeAdded()) {
				return;
			}
			String status = getString("STATUS");
			if("CZP".equalsIgnoreCase(this.getThisMboSet().getApp())){
				if("取消".equals(status) || "关闭".equals(status)){	
					setFlag(7L, true);
					return ;
				}
			}
			if("BZ_CZP".equalsIgnoreCase(this.getThisMboSet().getApp())){
				if("取消".equals(status) || "活动".equals(status)|| "关闭".equals(status)){	
					setFlag(7L, true);
					return ;
				}
			}
			String personId = getUserInfo().getPersonId();
			MboSetRemote adminSet = getMboSet("$groupuser", "GROUPUSER","groupname in('MAXADMIN') and userid= '" + personId + "'");
			if (!adminSet.isEmpty()) {
				return;
			}
			/*
			long l_id = getUniqueIDValue();
			MboSetRemote msr_instance = getMboSet("instance", "WFINSTANCE","ownertable='OPTICKET' and ownerid='" + l_id+ "' and active = 1");
			if (!msr_instance.isEmpty()) {
				String sqlexp = "ownerid='"+ l_id+ "' and ownertable='OPTICKET' and assignstatus='ACTIVE' and assigncode='"+ personId + "'";
				MboSetRemote mbosetremote = getMboSet("assigncode","WFASSIGNMENT", sqlexp);
				if (mbosetremote.isEmpty()) {
					
				}
			}
			*/
		} catch (RemoteException e1) {
			e1.printStackTrace();
		}
	}

	public void setBzczpProperty(MboRemote mboremote) throws RemoteException,
			MXException {
		mboremote.setValue("DATATYPE", "标准操作票", 11L);
		mboremote.setValue("STATUS", "草稿", 11L);
	}

	public void add() throws MXException, RemoteException {
		super.add();
		Date currentDate = ((AppService) getMboServer()).getMXServer()
				.getDate();
		String userid = getUserInfo().getPersonId();
		String appname = this.getThisMboSet().getApp();
		if ("BZ_CZP".equalsIgnoreCase(appname)) {
			setBzczpProperty(this);
		} else {
			setValue("STATUS", "拟票", 11L);
		}
		setValue("STATUSDATE", currentDate, 11L);
		setValue("REPORTEDBYID", userid, 11L);
		setValue("SITEID", getInsertSite(), 11L);
		setValue("ORGID", getInsertOrganization(), 11L);

	}

	public void appValidate() throws MXException, RemoteException {
		super.appValidate();
	}

	public void save() throws MXException, RemoteException {
		super.save();
		String appname = this.getThisMboSet().getApp();
		// String datatype=getString("DATATYPE");
		// && !"标准操作票".equals(datatype)
		if (!"BZ_CZP".equalsIgnoreCase(appname)) {
			String opticket = getString("opticket");
			if (opticket == null || "".equals(opticket)) {
				generateKey();
			}
		}
		super.save();
	}


	public void delete(long accessmodifier) throws MXException, RemoteException {
		getMboSet("OPTASK").deleteAll(accessmodifier);
		super.delete(accessmodifier);
	}

	public void undelete() throws MXException, RemoteException {
		getMboSet("OPTASK").undeleteAll();
		super.undelete();
	}

	public void generateKey() throws RemoteException, MXException {
		SimpleDateFormat dateFormat = new SimpleDateFormat("yyyyMMdd");
		String prefix = dateFormat.format(new Date());
		MboSetRemote tmpSet = getMboSet("$max_OPTICKET", "OPTICKET",
				" OPTICKET like '" + prefix + "%'");
		tmpSet.setOrderBy("OPTICKET desc");
		tmpSet.reset();
		MboRemote maxMbo = tmpSet.getMbo(0);
		String sufix = "";
		String pxsqnum = "";
		if (maxMbo != null) {
			pxsqnum = maxMbo.getString("OPTICKET");
			if (pxsqnum != null && pxsqnum.length() > 0) {
				pxsqnum = pxsqnum.substring(8);
				DecimalFormat format = new DecimalFormat("0000");
				sufix = format.format(Integer.parseInt(pxsqnum) + 1);
			}
		}
		pxsqnum = prefix;
		if (sufix != null && sufix.length() > 0) {
			pxsqnum += sufix;
		} else {
			pxsqnum += "0001";
		}
		setValue("OPTICKET", pxsqnum, 2L);
	}

	public MboRemote duplicate() throws MXException, RemoteException {
		if (!(isHashSetLoaded)) {
			loadSkipFieldCopyHashSet();
		}
		MboRemote mboremote = copy(getThisMboSet());
		MboSetRemote optaskMboSet = mboremote.getMboSet("OPTASK");
		MboSetRemote oldaskMboSet = this.getMboSet("OPTASK");
		if (!oldaskMboSet.isEmpty()) {
			oldaskMboSet.copy(optaskMboSet);
		}
		return mboremote;
	}

	/***
	 * 转标准票
	 * 
	 * @return
	 * @throws MXException
	 * @throws RemoteException
	 */
	public MboRemote transBzczpSave() throws MXException, RemoteException {
		if (!(isHashSetLoaded)) {
			loadSkipFieldCopyHashSet();
		}
		MboRemote mboremote = copy(getThisMboSet());
		setBzczpProperty(mboremote);
		MboSetRemote optaskMboSet = mboremote.getMboSet("OPTASK");
		MboSetRemote oldaskMboSet = this.getMboSet("OPTASK");
		if (!oldaskMboSet.isEmpty()) {
			oldaskMboSet.copy(optaskMboSet);
		}
		mboremote.setValue("STATUS", "活动", 11L);
		return mboremote;
	}

	protected boolean skipCopyField(MboValueInfo mbovalueinfo)
			throws RemoteException, MXException {
		return skipFieldCopy.contains(mbovalueinfo.getName());
	}

	public static HashSet getHashSet() throws MXException, RemoteException {
		return skipFieldCopy;
	}

	private static void loadSkipFieldCopyHashSet() throws MXException,
			RemoteException {
		isHashSetLoaded = true;
		skipFieldCopy.add("SITEID");
		skipFieldCopy.add("ORGID");
		skipFieldCopy.add("OPTICKET");
		skipFieldCopy.add("STATUS");
		skipFieldCopy.add("STATUSDATE");
		skipFieldCopy.add("OPTICKET");
		skipFieldCopy.add("REPORTEDBYID");
		skipFieldCopy.add("CUPERSONNAME");
		skipFieldCopy.add("REMARKDESC_LONGDESCRIPTION");

	}

	protected MboSetRemote getStatusHistory() throws MXException,
			RemoteException {
		return getMboSet("OPTICKETSTATUS");
	}

	protected StatusHandler getStatusHandler() {
		return new OptickeStatusHandler(this);
	}

	public String getStatusListName() {
		return "OPTICSTATUS";
	}
	

	public String getAttributeName() throws MXException, RemoteException {
		return "OPTICKETID";
	}

}
